package com.moqianfan.mcuassistant.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.moqianfan.mcuassistant.R;
import com.moqianfan.mcuassistant.adapter.CommDataAdapter;
import com.moqianfan.mcuassistant.bean.Collec;
import com.moqianfan.mcuassistant.database.CollecDao;
import com.moqianfan.mcuassistant.utils.LogUtil;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnItemClick;

import static com.mob.MobSDK.getContext;

/**
 * Created by Administrator on 2017/6/20.
 * 我的收藏
 */

public class CollecActivity extends BaseActivity {
    @BindView(R.id.line_null)
    LinearLayout lineNull;
    @BindView(R.id.list_collec)
    ListView listCollec;

    private List<Collec> collecs;
    private CommDataAdapter<Collec> adapter;
    private CollecDao collecDao;
    private String TAG = "CollecActivity";

    @Override
    public void findViews() {
        setContentView(R.layout.activity_collec);
    }

    @Override
    public void initViews() {
        collecs = new ArrayList<>();
        collecDao = appConfig.getDaoSession().getCollecDao();
    }

    @Override
    public void initDatas() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        collecs = collecDao.loadAll();
        LogUtil.e(TAG, "收藏总数=" + collecs.size());
        if (collecs.size() <= 0) {
            lineNull.setVisibility(View.VISIBLE);
        } else {
            lineNull.setVisibility(View.GONE);
            if (null == adapter) {
                adapter = new CommDataAdapter<Collec>(collecs, R.layout.view_item_wx) {
                    @Override
                    public void bindView(ViewHolder holder, Collec obj) {
                        if (!TextUtils.isEmpty(obj.getPicUrl()))
                            Picasso.with(getContext()).load(obj.getPicUrl()).placeholder(R.drawable.default_wx)
                                    .resize(100, 100).into((ImageView) holder.getView(R.id.image_item_url));
                        else
                            holder.setVisibility(R.id.image_item_url, View.GONE);

                        holder.setText(R.id.txt_item_title, obj.getTitle());

                        holder.setText(R.id.txt_item_desc, obj.getDesc());
                    }
                };
                listCollec.setAdapter(adapter);
            } else {
                adapter.setmData(collecs);
            }
        }
    }

    @OnClick({R.id.image_close, R.id.image_coll})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.image_close:
                finish();
                break;
            case R.id.image_coll:
                break;
        }
    }

    @OnItemClick(R.id.list_collec)
    public void OnItemClick(AdapterView<?> view, View view1, int i, long l) {
        Collec collec = collecs.get(i);
        LogUtil.e(TAG, "点击=" + collec.toString());
        Intent inten = new Intent(CollecActivity.this, WebInfoActivity.class);

        inten.putExtra("title", collec.getTitle());
        if (!TextUtils.isEmpty(collec.getHtml()))
            inten.putExtra("html", collec.getHtml());
        else {
            inten.putExtra("url", collec.getUrl());
            if (collec.getUrl().contains("tech.qq.com"))
                inten.putExtra("flag", 0);
        }
        startActivity(inten);
    }
}
